<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/conf/config.php';

$page	= isset($_POST['page']) ? $_POST['page'] : 1;
$limit	= isset($_POST['rows']) ? $_POST['rows'] : 20;
$sidx	= isset($_POST['sidx']) ? $_POST['sidx'] : 'SEQNO';
$sord	= isset($_POST['$sord']) ? $_POST['$sord'] : 'asc';

$cond 	= isset($_POST['cond']) ? $_POST['cond'] : '';
$word 	= isset($_POST['word']) ? $_POST['word'] : '';

$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());

mysql_select_db($dbname) or die("Error conecting to db.");

if($_POST['cond'] != '' && $_POST['word'] != '')
	$countQRY = "select count(*) as count from qr_data where " . $cond . " like '" . $word . "%'";
else
	$countQRY = "select count(*) as count from qr_data";

$result = mysql_query($countQRY);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}

if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)

if($_POST['cond'] != '' && $_POST['word'] != '') {
	$selectQRY  = "select * from qr_data where " . $cond . " like '" . $word . "%' ";
	$selectQRY .= "ORDER BY $sidx $sord LIMIT $start, $limit";
}
else  {
	$selectQRY  = "select * from qr_data ";
	$selectQRY .= "ORDER BY $sidx $sord LIMIT $start, $limit";	
}

$result = mysql_query( $selectQRY ) or die("Couldn t execute query.".mysql_error());

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $responce->rows[$i]['SEQNO']=$row['SEQNO'];
    $responce->rows[$i]['cell'] = array($row['SEQNO'],$row['NEWSI'],$row['NEWGU'],$row['NEWDONG'],$row['NEWBUNJI'],
    					$row['USED'],$row['UNIT'],$row['CPIC'],$row['CDATE'], $row['LAT'],$row['LNG'],$row['PNG']);
    $i++;
}        
echo json_encode($responce); 